package com.cloudcc.mobile.weight;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SlideButton extends LinearLayout {

	private Context mContext;
	private TextView mTv;
	private ImageView mIv;

	private int expandResId;
	private int closedResId;
	private String expandText = "";
	private String closedText = "";

	/**
	 * 是否打开
	 */
	private boolean isExpand;

	public SlideButton(Context context) {
		this(context, null);
	}

	public SlideButton(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public SlideButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		initLayout();
	}

	/**
	 * 添加布局
	 */
	private void initLayout() {
		setGravity(Gravity.CENTER_HORIZONTAL);

		mTv = new TextView(mContext);
		mTv.setLayoutParams(new LayoutParams(
				LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		mTv.setGravity(Gravity.CENTER);

		mIv = new ImageView(mContext);
		mIv.setLayoutParams(new LayoutParams(
				LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));

	}

	public void setText(String text) {
		mTv.setText(text);
	}

	public void setIcon(int expandResId, int closedResId) {
		this.expandResId = expandResId;
		this.closedResId = closedResId;
	}

	public void setToggle(int[] resids, String[] texts) {
		this.expandResId = resids[0];
		this.expandText = texts[0];
		this.closedResId = resids[1];
		this.closedText = texts[1];
	}

	public void toggle() {
		if (isExpand) {
			mIv.setImageResource(closedResId);
			mTv.setText(closedText);
		} else {
			mIv.setImageResource(expandResId);
			mTv.setText(expandText);
		}
		isExpand = !isExpand;
	}

	public float dp2px(float dp) {
		return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
				getResources().getDisplayMetrics());
	}

	public float sp2px(float sp) {
		return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
				getResources().getDisplayMetrics());
	}
}
